<template>
  <div class="login">
    <div class="login-container">
      <div class="logo-wrap">
        <img src="../../assets/login-logo.png" class="logo-img" />
        <span class="sys-name">XXX管理平台</span>
      </div>
      <img src="../../assets/footer.png" class="footer-img" />
      <div class="login-wrap">
        <div v-if="activeName != 'dingTalkPane' && activeName != 'findPwd' && activeName != 'wxPane'">
          <el-tabs v-model="activeName">
            <!-- <el-tab-pane label="密码登录" name="accountPane" /> -->
            <el-tab-pane label="工号登录" name="staffPane" />
            <el-tab-pane label="手机验证登录" name="phonePane" />
          </el-tabs>
          <div class="pane-content">
            <LogByPrimary v-if="activeName === 'accountPane'" :redirect="redirect" />
            <LogByPhone v-if="activeName === 'phonePane'" :redirect="redirect" />
            <LogByStaffNum v-if="activeName === 'staffPane'" :redirect="redirect" />
          </div>
          <!-- <div class="pane-footer">
          <div class="flex">
            <div class="flex-center">
              <span style="color: #666666">其他登录：</span>
              <img src="@/assets/iconDingTalk.png" class="logImg" @click="activeName = 'dingTalkPane'" />
              <img src="@/assets/iconWx.png" class="logImg" @click="activeName = 'wxPane'" />
            </div>
            <div class="logTip" @click="activeName = 'findPwd'">忘记密码</div>
          </div>
        </div> -->
        </div>

        <div v-if="activeName === 'dingTalkPane'">
          <div class="flex-center logTitle">钉钉登录</div>
          <LogByDingTalk />
          <div class="flex-center logTip" @click="activeName = 'staffPane'">返回其他登录</div>
        </div>

        <div v-if="activeName === 'wxPane'">
          <div class="flex-center logTitle">微信登录</div>
          <LogByWx />
          <div class="flex-center logTip" @click="activeName = 'staffPane'">返回其他登录</div>
        </div>

        <div v-if="activeName === 'findPwd'">
          <div class="flex-center logTitle">找回密码</div>
          <FindPwd @backToLogin="activeName = 'staffPane'" />
          <div class="flex-center logTip" @click="activeName = 'staffPane'">返回登录</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import LogByPhone from './logByPhone.vue'
import LogByPrimary from './logByPrimary.vue'
import LogByDingTalk from './logByDingTalk.vue'
import LogByWx from './logByWx.vue'
import LogByStaffNum from './logByStaffNum'
import FindPwd from './findPwd.vue'
import { param2Obj } from '@/utils'

export default {
  name: 'Login',
  components: {
    LogByPrimary,
    LogByWx,
    FindPwd,
    LogByPhone,
    LogByStaffNum,
    LogByDingTalk
  },
  data() {
    return {
      activeName: 'staffPane',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        if (route) {
          const state = this._getQueryVariable('state')
          const code = this._getQueryVariable('code')
          if (state && state === 'dingtalk') {
            this.dingTalkLogin(code)
          }
          if (state && state === 'wechat') {
            this.weChatLogin(code)
          }
        }
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // 钉钉撒吗登录
    async dingTalkLogin(code) {
      // this.$store
      //   .dispatch('user/loginByDingTalk', code)
      //   .then(() => {
      //     let url = window.location.href //获取当前页面的url
      //     if (url.indexOf('?') != -1) {
      //       url = url.split('/login')[0].replace(/(\?|#)[^'"]*/, '')
      //       //判断是否存在参数
      //       // url = url.replace(/(\?|#)[^'"]*/, '')
      //       window.history.pushState({}, 0, url)
      //     }
      //     //然后跳转首页
      //     this.$router.replace({ path: '/' })
      //   })
      //   .catch(() => {
      //     this.$router.replace({ path: '/' })
      //   })
      // return false
      try {
        await this.$store.dispatch('user/loginByDingTalk', code)
        let url = window.location.href //获取当前页面的url
        if (url.indexOf('?') != -1) {
          url = url.split('/login')[0].replace(/(\?|#)[^'"]*/, '')
          //判断是否存在参数
          // url = url.replace(/(\?|#)[^'"]*/, '')
          window.history.pushState({}, 0, url)
        }
        //然后跳转首页
        this.$router.replace({ path: '/' })
        return false
      } catch (error) {
        this.$router.replace({ path: '/' })
      }
    },
    // 微信扫码登录
    async weChatLogin(code) {
      try {
        await this.$store.dispatch('user/loginByWeChat', code)
        let url = window.location.href //获取当前页面的url
        if (url.indexOf('?') != -1) {
          url = url.split('/login')[0].replace(/(\?|#)[^'"]*/, '')
          //判断是否存在参数
          // url = url.replace(/(\?|#)[^'"]*/, '')
          window.history.pushState({}, 0, url)
        }
        //然后跳转首页
        this.$router.replace({ path: '/' })
      } catch (error) {
        console.log(error)
        this.$router.replace({ path: '/' })
      }
      return false
    },
    // 判断url参数
    _getQueryVariable(variable) {
      let query = param2Obj(window.location.href)
      if (Object.prototype.hasOwnProperty.call(query, variable)) {
        return query[variable]
      }
      return false
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$cursor: gray;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 46px;
    input {
      -webkit-appearance: none;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      border: 1px solid #ecf0f6;
      background: #f8f8f8;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: #000 !important;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
.login {
  background-color: #f8fcff;
}
.login-container {
  width: 1280px;
  height: 100vh;
  min-height: 700px;
  background: url('../../assets/login.png') no-repeat top center;
  background-size: 1920px;
  left: 50%;
  margin-left: -640px;
  position: relative;
  .logo-wrap {
    position: absolute;
    left: 358px;
    top: 60px;
    display: flex;
    align-items: center;
    .logo-img {
      width: 118px;
      height: 37px;
    }
    .sys-name {
      font-size: 22px;
      color: #333;
      font-family: 'Microsoft YaHei';
      &::before {
        content: '';
        display: inline-block;
        vertical-align: middle;
        height: 20px;
        border: 1px solid #eee;
        margin: 16px;
      }
    }
  }
  .footer-img {
    position: fixed;
    bottom: 0;
    width: 100vw;
    height: auto;
  }
  .login-wrap {
    background: #fff;
    border-radius: 10px;
    width: 385px;
    height: 411px;
    padding: 53px 24px 61px;
    box-shadow: 0px 3px 13px 0px rgba(153, 153, 153, 0.35);
    position: absolute;
    top: 180px;
    left: 50%;
    margin-left: 260px;

    .pane-content {
      margin-top: 20px;
    }
    .pane-footer {
      .logImg {
        margin-right: 5px;
        border: 50%;
        width: 24px;
        height: 24px;
        cursor: pointer;
      }
    }

    .logTitle {
      height: 18px;
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #333333;
      line-height: 46px;
    }
    .logTip {
      font-size: 12px;
      cursor: pointer;
      font-weight: bold;
      color: #409eff;
      font-family: Microsoft YaHei;
    }
  }
  ::v-deep.el-tabs__nav-scroll {
    display: flex;
    justify-content: center;
  }

  .title {
    font-family: Microsoft YaHei;
    font-weight: bold;
    font-size: 18px;
    line-height: 46px;
    color: #409eff;
  }
}
</style>
